var app = getApp();
Page(Object.assign({}, {
    data:{
        isappyed : 0,
        options : null,
        inited : false,
        form : {},
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
	    app.com.getLocation(self,function(res){
	        app.com.auth(function(user){
	            app.com.http('user_v1','POST',{op:'join'},0,false,'','',function(res){
	                if (!res.data.errno) {
	                    self.setData({
	                        sets : res.data.data.sets,
	                        allsort : res.data.data.allsort,
	                        isappyed : res.data.data.isappyed,
	                    },function(){
	                        self.setData({
	                            inited : true,
	                        });
	                    });
	                    // 关闭下拉刷新
	                    wx.stopPullDownRefresh();
	                }
	            });
	        },self);
	    });

        wx.setNavigationBarTitle({
          title: '合作',
        });

    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '合作',
            path: '',
        }
    },
    sort(e){
    	var self = this;
    	for (let i = 0, il = self.data.allsort; i< il.length; i++) {
    		if( i == e.detail.value ){
                self.setData({
                    'form.sortname' : il[i].name,
                    'form.sortid' : il[i].id,
                });
    		}
    	}
        self.setData({
            'form.sort' : true,
        });
    },
    getAddress(e){
    	var self = this;
    	wx.chooseLocation({
    		success : function(res){
                self.setData({
                    'form.lat' : res.latitude,
                    'form.lng' : res.longitude,
                    'form.address' : res.address,
                });
    		}
    	});
    },
    inputvalue(e){
    	var self = this;
    	var type = e.currentTarget.dataset.type;
    	var value = e.detail.value;
    	var name = 'form.'+type;
        self.setData({
            [name] : value,
        });
    },
    sub(e){
    	var self = this;
    	if( !self.data.form.name ) {
    		app.com.toast('请填入姓名');return false;
    	}
    	if( !self.data.form.tel ) {
    		app.com.toast('请填入电话');return false;
    	}
    	if( !app.com.verify('mobile',self.data.form.tel) ) {
    		app.com.toast('请填入正确的手机号码');return false;
    	}    	
    	if( !self.data.form.pass ) {
    		app.com.toast('请设置登录密码');return false;
    	}
    	if( !self.data.form.shopname ) {
    		app.com.toast('请填写名称');return false;
    	}
    	if( !self.data.form.sortname ) {
    		app.com.toast('请选择业务类型');return false;
    	}
    	if( !self.data.form.lat ) {
    		app.com.toast('请选择位置');return false;
    	}
    	var form = JSON.stringify( self.data.form );
    	if( self.data.sets.applyneed*1 > 0 ){
    		var postdata = {
    			op : 'payapply',
    			form:form
    		};
	        app.com.http('user_v1','POST',postdata,0,true,'','',function(res){
	            if (!res.data.errno) {
                    wx.requestPayment({
						'appId' : res.data.data.appId,
                        'timeStamp': res.data.data.timeStamp,
                        'nonceStr': res.data.data.nonceStr,
                        'package': res.data.data.package,
                        'signType': 'MD5',
                        'paySign': res.data.data.paySign,
                        'success': function (payres) {
                            if( payres.errMsg == 'requestPayment:ok' ){
				            	var str = '已提交申请';
				            	if( res.data.data.status == 2 ) {
				            		str = '入驻成功';
				            	}
				                app.com.toast(str,'success',function(){
				                	self.setData({
				                		form : {},
				                		isappyed : res.data.data.status,
				                	});
				                	if( res.data.data.status == 2 ){
				                		wx.navigateTo({
				                			url : '/zofui_nearly/pages/admin/login'
				                		});
				                	}
				                });
                            }
                        },
                        'fail': function (payres) {
                            console.log( '失败' );
                        }
                    });
	            }else{
	            	app.com.alert(res.data.message);
	            }
	        });
    	}else{
	        app.com.http('user_v1','POST',{op:'subjoin',form:form},0,true,'','',function(res){
	            if (!res.data.errno) {
	            	var str = '已提交申请';
	            	if( res.data.data.status == 2 ) {
	            		str = '入驻成功';
	            	}
	                app.com.toast(str,'success',function(){
	                	self.setData({
	                		form : {},
	                		isappyed : res.data.data.status,
	                	});
	                	if( res.data.data.status == 2 ){
	                		wx.navigateTo({
	                			url : '/zofui_nearly/pages/admin/login'
	                		});
	                	}
	                });
	            }else{
	            	app.com.toast( res.data.message );
	            }
	        });
    	}
    },
    onPullDownRefresh: function () {
        this.onLoad();
        wx.stopPullDownRefresh();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e,this.data.tid );
    },
    redirectto : function( e ){
        app.com.redirectto( e,this.data.tid );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    }
}))